/**
 * 文件名    ： Hebing.java
 * 版权       : XX科技有限公司。
 * 描述       : <描述>
 * 修改时间：2019年9月7日
 * 修改内容：<修改内容>
 */
package com.example.demo.application.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class Hebing {


		public static void main(String[] args) throws IOException {
			String PATH = "";
			PATH = "D:/home/novel_data/00/02";
			File file_tmp = new File(PATH);
			if (!file_tmp.exists()) {
				return;
			}
			else {
				PATH = file_tmp.getCanonicalPath() + "/";
			}
			
			File fileDst = new File(PATH  + "file_dir_dst.txt");
			createFile(fileDst);
			
			FileInputStream fin = new FileInputStream(PATH  + "file_dir_dst.txt");
			long lineNumber=1;
			File file3 = new File(PATH  + "mengqin.txt");
			createFile(file3);
			BufferedReader buffReader=new BufferedReader(new InputStreamReader(fin));
			String lineInfo = buffReader.readLine();
			List<String> lis = new ArrayList<String>();
			while(lineInfo!= null){
				if (lis.contains(lineInfo)) {
					System.out.println("第:"+lineNumber+"行数据是：" +lineInfo) ;
					lineInfo = buffReader.readLine();
					continue;
				}
				lis.add(lineInfo);
				lineNumber++;
				File file = new File(PATH  + lineInfo);
				if (!file.exists()) {
					lineInfo = buffReader.readLine();
					continue;
				}
				//合并
				writeTxtFile("\r\n\r\n\r\n" + lineInfo +"\r\n\r\n"+ readFile(file), file3);
				
				lineInfo = buffReader.readLine();
			}
			System.out.println(lineNumber);
			
		}

		/**
		 * 按行读取文件
		 */
		private static String readFile(File file) {
//		        File file = new File(filename);
		        InputStream is = null;
		        Reader reader = null;
		        StringBuilder sb = new StringBuilder();
		        BufferedReader bufferedReader = null;
		        try {
		            is = new FileInputStream(file);
		            reader = new InputStreamReader(is);
		            bufferedReader = new BufferedReader(reader);
		            String line = null;
		            while ((line = bufferedReader.readLine()) != null) {
		            	line = line.replace("&nbsp;&nbsp;", " ");
		            	line = line.replace("&nbsp;", " ");
		            	line = line +"\r\n";
		            	sb.append(line);
		            }
		            return sb.toString();

		        } catch (FileNotFoundException e) {
		            e.printStackTrace();
		        } catch (IOException e) {
		            e.printStackTrace();
		        } finally {
		            try {
		                if (null != bufferedReader)
		                    bufferedReader.close();
		                if (null != reader)
		                    reader.close();
		                if (null != is)
		                    is.close();
		            } catch (IOException e) {
		                e.printStackTrace();
		            }
		        }
				return null;
		}

		public static void writeTxtFile(String content, File fileName) throws IOException {
			RandomAccessFile randomFile = null;
			try {
				// 打开一个随机访问文件流，按读写方式
				randomFile = new RandomAccessFile(fileName, "rw");
				// 文件长度，字节数
				long fileLength = randomFile.length();
				// 将写文件指针移到文件尾。
				randomFile.seek(fileLength);
				String cont = content + "\r\n";
				randomFile.write(cont.getBytes("UTF-8"));
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (randomFile != null) {
					randomFile.close();
				}
			}
		}
		
		public static void createFile(File fileName) throws IOException {
			try {
				if (!fileName.exists()) {
					fileName.createNewFile();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		

}
